package com.mlizhi.modules.spec.util;

import com.mlizhi.base.Utils;
import com.mlizhi.utils.Constants;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2015-03-24.
 */
public class JsonUtil {
    /**
     * 获取jsonObject头部
     * @param jsonObjectString
     * @return
     */
    private static JSONObject getHeaderJsonObject(String jsonObjectString){
        JSONObject headerJsonObject = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonObjectString);
            headerJsonObject = jsonObject.getJSONObject(Constants.RESULT_JSON_HEADER);
            return headerJsonObject;
        } catch (JSONException e) {
//            e.printStackTrace();
            Utils.E("parse jsonObject error!!!", e);
        }
        return null;
    }

    /**
     * 获取jsonObject主体
     * @param jsonObjectString
     * @return
     */
    public static JSONObject getBodyJsonObject(String jsonObjectString){
        JSONObject bodyJsonObject = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonObjectString);
            bodyJsonObject = jsonObject.getJSONObject(Constants.RESULT_JSON_BODY);
            return bodyJsonObject;
        } catch (JSONException e) {
//            e.printStackTrace();
            Utils.E("parse jsonObject error!!!",e);
        }
        return null;
    }
    
    /**
     * 获取jsonObject头部code
     * @param jsonObjectString
     * @return
     */
    public static String getHeaderCode(String jsonObjectString) {

        String code = "";
        try {
            JSONObject headerJsonObject = getHeaderJsonObject(jsonObjectString);
            code = headerJsonObject.getString(Constants.RESULT_JSON_HEADER_CODE);
            return code;
        } catch (Exception e) {
//            e.printStackTrace();
            Utils.E("parse jsonObject error!!!",e);
        }
        return "";
    }


    /**
     * 获取jsonObject头部error info
     * @param jsonObjectString
     * @return
     */
    public static String getHeaderErrorInfo(String jsonObjectString) {

        String description = "";
        try {
            JSONObject headerJsonObject = getHeaderJsonObject(jsonObjectString);
            description = headerJsonObject.getString("description");
            return description;
        } catch (Exception e) {
//            e.printStackTrace();
            Utils.E("parse jsonObject error!!!",e);
        }
        return "服务器发生未知错误！！！";
    }




    /**
     * 根据资讯数组json串，转换成资讯列表Map
     * @param brandString
     * @return
     */
    public static List<HashMap<String,String>> getListContentMap(JSONObject contentJsonObject) {
    	List<HashMap<String,String>> contentMapList = new ArrayList<HashMap<String,String>>();
    	HashMap<String,String> contentMap = null;
        
    	if(contentJsonObject == null){
            return null;
        }
        try {
            JSONArray contentJsonArr = contentJsonObject.getJSONArray("info");
            if(contentJsonArr != null) {
                JSONObject contentObj = null;
                for(int i=0;i<contentJsonArr.length();i++) {
                	contentObj = (JSONObject) contentJsonArr.get(i);
                	contentMap = new HashMap<String,String>();
                	contentMap.put(Constants.CONTENT_ITEM_ID, contentObj.getString("id"));
                	contentMap.put(Constants.CONTENT_ITEM_IMAGE, contentObj.getString("imageUrl"));
    				contentMap.put(Constants.CONTENT_ITEM_PRAISE_NUM, contentObj.getString("likeCount"));
    				contentMap.put(Constants.CONTENT_ITEM_TITLE, contentObj.getString("title"));
    				contentMap.put(Constants.CONTENT_ITEM_SUMMARY, contentObj.getString("summary"));
    				contentMap.put(Constants.CONTENT_ITEM_VIEW_NUM, contentObj.getString("lookCount"));
    				contentMap.put(Constants.CONTENT_ITEM_TYPE, contentObj.getString("infoType"));
    				contentMap.put(Constants.CONTENT_ITEM_MODULE_TYPE, contentObj.getString("moduleType"));
    				contentMapList.add(contentMap);
                }
                return contentMapList;
            }
        } catch (Exception e) {
//            e.printStackTrace();
            Utils.E("parse jsonObject error!!!",e);
        }
        return null;
    }

    
    
}
